package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.HealthClock;
import com.example.service.HealthClockService;
import com.example.vo.DataView;
import com.example.vo.HealthClockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HealthClockController {

    @Autowired
    private HealthClockService healthClockService;
    /**
     * 查询所有打卡记录 带有模糊查询条件 带有分页
     * @param healthClockVo
     * @return
     */
    @RequestMapping("/listHealthClock")
    public DataView listHealthClock(HealthClockVo healthClockVo){

        IPage<HealthClock> page = new Page<>(healthClockVo.getPage(),healthClockVo.getLimit());
        QueryWrapper<HealthClock> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(healthClockVo.getUsername()!=null,"username",healthClockVo.getUsername());
        queryWrapper.eq(healthClockVo.getPhone()!=null,"phone",healthClockVo.getPhone());
        healthClockService.page(page, queryWrapper);
        DataView dataView=new DataView(page.getTotal(),page.getRecords());
        return dataView;
    }

    /**
     * 添加健康打卡记录数据
     * @return
     */
    @RequestMapping("/addHealthClock")
    public DataView addHealthClock(HealthClock healthClock){
        boolean b = healthClockService.saveOrUpdate(healthClock);
        DataView dataView = new DataView();
        if(b){
            dataView.setCode(200);
            dataView.setMsg("添加成功");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("添加失败");
        return dataView;
    }


    /**
     * 删除数据行
     * @param id
     * @return
     */
    @RequestMapping("/deleteHealthClockById")
    public DataView deleteHealthClockById(Integer id){
        healthClockService.removeById(id);
        DataView dataView=new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除成功");
        return dataView;
    }
}
